<?php

namespace app\common\model;

use think\Model;

/**
 * 会员余额日志模型
 */
class MoneyLog extends Model
{

    // 表名
    protected $name = 'user_money_log';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = '';
    // 追加属性
    protected $append = [
        'createtime_text',
        'goods_name_text',
        'phone_text',
        'zhichu_text'
    ];

    public function getCreateTimeTextAttr($value,$data)
    {
       if(isset($data['createtime'])){
           $value = date('Y-m-d H:i:s',$data['createtime']);
       }
       return $value;
    }

    public function getGoodsNameTextAttr($value,$data)
    {

        if(isset($data['goods_id']) && isset($data['memo'])){
            $goodsname = Packages::where(['id'=>$data['goods_id']])->value('name');
            $value = $goodsname;
        }
        return $value;
    }
    public function getPhoneTextAttr($value,$data)
    {
        if(isset($data['order_id'])){
            $phone = Order::where(['id'=>$data['order_id']])->value('phone');
            $value = $phone;
        }
        return $value;
    }

    public function getZhichuTextAttr($value,$data)
    {
        if(isset($data['order_id'])){
            if($data['money'] < 0){
                $WithdrawInfo = UserWithdrawApply::where(['id'=>$data['order_id']])->find();
                if($WithdrawInfo){
                    if($WithdrawInfo['status']== 1){
                        $value = "待打款";
                    }elseif ($WithdrawInfo['status']==2){
                        $value = "已打款";
                    }elseif($WithdrawInfo['status']==4){
                        $value = "打款失败：".$WithdrawInfo['reason'];
                    }
                }else{
                    $orderInfo = Order::where(['id'=>$data['order_id']])->find();
                    if($orderInfo){
                        $value = $orderInfo['order_id'];
                    }
                }
            }

        }
        return $value;
    }

}
